package com.parking.carsystem.parkingchargesystem.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.common.Constant;
import com.parking.carsystem.parkingchargesystem.module.UserInfoModel;
import com.parking.carsystem.parkingchargesystem.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AuthenticationActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.setting)
    TextView setting;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.title_bg)
    RelativeLayout titleBg;
    @BindView(R.id.inpu_name)
    EditText inpuName;
    @BindView(R.id.line)
    View line;
    @BindView(R.id.id_card)
    EditText idCard;
    @BindView(R.id.line1)
    View line1;
    @BindView(R.id.search_paking)
    TextView searchPaking;
    @BindView(R.id.no_authentication)
    RelativeLayout noAuthentication;
    @BindView(R.id.medal_image)
    ImageView medalImage;
    @BindView(R.id.medal_status)
    TextView medalStatus;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.idcard)
    TextView idcard;
    @BindView(R.id.authentication)
    RelativeLayout authentication;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_authentication;
    }

    @Override
    public void initView() {
        text.setText("实名认证");
        searchPaking.setOnClickListener(this);
    }

    @Override
    public void initData() {
        getUserInfo();
    }

    private void getPersonAuthenTication() {
        showLoading();
        String inputName = inpuName.getText().toString();
        String idCardName = idCard.getText().toString();
        if (TextUtils.isEmpty(inputName) || TextUtils.isEmpty(idCardName)) {
            showToast("请输入正确的信息");
            return;
        }
        if (!Utils.isIDcard(idCardName)) {
            showToast("请输入正确的身份证号码");
            return;
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("realName", inputName);
            jsonObject.put("idCardNo", idCardName);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkGo.<String>post(Constant.url + Constant.USERREALNAMEAUTH)
                .upJson(jsonObject.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        stopLoading();
                        boolean parseJson = parseJson(response.body());
                        if (parseJson) {
                            showToast("认证成功");
                            finish();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        stopLoading();
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_paking:
                getPersonAuthenTication();
                break;
        }
    }

    @Override
    public void updateData(UserInfoModel userInfoModel) {
        if (TextUtils.isEmpty(userInfoModel.data.idCardNo) || TextUtils.isEmpty(userInfoModel.data.realName)) {
            noAuthentication.setVisibility(View.VISIBLE);
            authentication.setVisibility(View.GONE);
        } else {
            name.setText(userInfoModel.data.realName);
            idcard.setText(userInfoModel.data.idCardNo);
            noAuthentication.setVisibility(View.GONE);
            authentication.setVisibility(View.VISIBLE);
        }
    }
}
